/**
 * File: VendaHeaderController.java
 * Data: 11.11.2010
 * Objective: Criar a classe controller da entidade Venda Header.
 *
 * Copyright 2010 S2DZ CORPORATION
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package controller;

import java.sql.Date;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import modelo.VendaHeaderModelo;

/**
 * Controller da entidade Venda Header.
 * @author Paulo Ilenga
 */
public class VendaHeaderController extends ControllerGeneric
{
    private VendaHeaderModelo modelo;

    @Override
    public boolean save(Object object)
    {
        modelo = (VendaHeaderModelo) object;

        connect();

        sql = "INSERT INTO tbl_venda_header(num_doc, id_cliente, moeda, taxa_cambio, data_registo) VALUES (?, ?, ?, ?, ?)";

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setString(1, modelo.getNumDoc());
            ps.setInt(2, modelo.getIdCliente());
            ps.setString(3, modelo.getMoeda());
            ps.setFloat(4, modelo.getTaxaCambio());
            ps.setDate(5, (Date) modelo.getDataRegisto());

            result = ps.execute();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(VendaHeaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            disconnect();
        }

        return result;
    }

    @Override
    public boolean update(Object object)
    {
        modelo = (VendaHeaderModelo) object;

        connect();

        sql = "UPDATE tbl_venda_header SET num_doc=?, id_cliente=?, moeda=?, taxa_cambio=?, data_registo=? WHERE id=?";

        try
        {
            ps = connection.prepareStatement(sql);

            ps.setString(1, modelo.getNumDoc());
            ps.setInt(2, modelo.getIdCliente());
            ps.setString(3, modelo.getMoeda());
            ps.setFloat(4, modelo.getTaxaCambio());
            ps.setDate(5, (Date) modelo.getDataRegisto());
            ps.setInt(6, modelo.getId());

            result = ps.execute();
        }
        catch (SQLException ex)
        {
            Logger.getLogger(VendaHeaderController.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            disconnect();
        }

        return result;
    }

}
